#!/bin/sh

if [ -f Build.PL ]; then
    perl Build.PL
    ./Build clean
    perl Build.PL
    ./Build
else 
    perl Makefile.PL
    make clean
    perl Makefile.PL
    make
fi

if [ -f Build.PL ]; then
    if ! [ -f META.yml ]; then
        ./Build distmeta
    fi
    VERSION=`grep "^version: " META.yml | sed 's/.*: //'`
    DISTNAME=`grep "^name: " META.yml | sed 's/.*: //'`
    DISTVNAME="$DISTNAME-$VERSION"
else
    VERSION=`grep "^VERSION = " Makefile | sed 's/.* = //'`
    DISTNAME=`grep "^DISTNAME = " Makefile | sed 's/.* = //'`
    DISTVNAME=`grep "^DISTVNAME = " Makefile | sed 's/.* = //'`
fi
ARCHCOUNT=`find blib/arch -type f ! -empty | wc -l`

rm -f $DISTVNAME.ppd

if [ -f Build.PL ]; then
    ./Build ppd
else
    make ppd
fi

if [ $ARCHCOUNT = 0 ]; then
    cat $DISTNAME.ppd \
        | egrep -v '(OS|ARCHITECTURE) NAME=' \
        > $DISTNAME.ppd.tmp
    mv -f $DISTNAME.ppd.tmp $DISTNAME.ppd
fi

cat $DISTNAME.ppd \
    | sed -e 's,\(CODEBASE HREF="\).*",\1./'$DISTVNAME'.tgz",' \
          -e 's,\(SOFTPKG NAME=.* VERSION="\).*",\1'$VERSION'",' \
    > $DISTVNAME.ppd

rm -f $DISTNAME.ppd

if [ -f Build.PL ]; then
    rm -f PPM-*.tar.gz
    mv -f blib/bindoc blib/man1
    mv -f blib/libdoc blib/man3
    tar zcvf $DISTVNAME.tgz blib
    # mv -f PPM-*.tar.gz $DISTVNAME.tgz
    # mv -f .tgz $DISTVNAME.tgz
else
    tar zcvf $DISTVNAME.tgz blib
fi
